package com.redlight.utils;//package com.atguigu.springboot.utils;
//
//import org.apache.commons.lang3.Validate;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.DisposableBean;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpSession;
//
///**
// * Created by chao on 2019/1/8.
// */
//@Component
//@Lazy(false)
//public class ContextHolder implements ApplicationContextAware, DisposableBean {
//    private static ApplicationContext applicationContext = null;
//    private static Logger LOGGER = LoggerFactory.getLogger(ContextHolder.class);
//
//    public ContextHolder() {
//    }
//
//    public static ApplicationContext getApplicationContext() {
//        assertContextInjected();
//        return applicationContext;
//    }
//
//    public static <T> T getBean(String name) {
//        assertContextInjected();
//        return (T) applicationContext.getBean(name);
//    }
//
//    public static <T> T getBean(Class<T> requiredType) {
//        assertContextInjected();
//        return applicationContext.getBean(requiredType);
//    }
//
//    public static void clearHolder() {
//        if(LOGGER.isDebugEnabled()) {
//            LOGGER.debug("清除ContextUtil中的ApplicationContext:" + applicationContext);
//        }
//
//        applicationContext = null;
//    }
//
//    public void setApplicationContext(ApplicationContext applicationContext) {
//        if(applicationContext != null) {
//            LOGGER.info("ContextUtil中的ApplicationContext被覆盖, 原有ApplicationContext为:" + applicationContext);
//        }
//
////        this.applicationContext = applicationContext;
//        if(applicationContext!=null){
//            LOGGER.info("applicationContext 不为空");
//        }
//    }
//
//    public void destroy() throws Exception {
//        clearHolder();
//    }
//
//    private static void assertContextInjected() {
//        Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义bean:ContextHolder.", new Object[0]);
//    }
//
//    public static HttpServletRequest getRequest() {
//        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
//        HttpServletRequest request = attr.getRequest();
//        return request;
//    }
//
////    public static SysUser getUser() {
////        try {
////            HttpSession session = getRequest().getSession();
////            return (SysUser)session.getAttribute("user");
////        } catch (Exception var1) {
////            var1.printStackTrace();
////            return null;
////        }
////    }
//}
